package com.latefly.skewwork.utils

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat

object PermissionUtils {

    private val permissionList = listOf<String>(
        Manifest.permission.BLUETOOTH_CONNECT,
        Manifest.permission.BLUETOOTH_SCAN,
        Manifest.permission.BLUETOOTH_ADVERTISE,

        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ANSWER_PHONE_CALLS,
        Manifest.permission.ACCESS_NETWORK_STATE,


        )

    fun checkBlePermission(context: Context, permission: String): Boolean {
        return ActivityCompat.checkSelfPermission(
            context,
            permission
        ) != PackageManager.PERMISSION_GRANTED
    }

    fun checkBlePermission(context: Context, permissions: List<String>): Boolean {
        var result = true
        permissions.forEach() {
            result = result && checkBlePermission(context, it)
        }
        return result
    }

    fun checkBlePermission(context: Context): Boolean {
        return checkBlePermission(context, permissionList)
    }

    fun requirePermission(activity: Activity, permissionName: String) {
        ActivityCompat.requestPermissions(
            activity,
            arrayOf(permissionName),
            //生成随机数
            (0..100000).random()
        )
    }

    fun checkPermissionWithRequire(activity: Activity, permissionName: String): Boolean {
        if (!checkBlePermission(activity.applicationContext, permissionName)) {
            requirePermission(activity, permissionName)
        }
        return checkBlePermission(activity.applicationContext, permissionName)
    }

    fun checkPermissionWithRequire(activity: Activity, permissionNames: List<String>): Boolean {
        var result = true
        permissionNames.forEach() {
            result = result && checkPermissionWithRequire(activity, it)
        }
        return result
    }

    fun checkPermissionWithRequire(activity: Activity): Boolean {
        var result = true
        permissionList.forEach() {
            result = result && checkPermissionWithRequire(activity, it)
        }
        return result
    }

}